﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using System;
using System.Diagnostics;
using System.IO;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        public IActionResult ShowViewerJS()
        {
            return View();
        }

        public IActionResult Download(string fileName)
        {
            var extension = Path.GetExtension(fileName);
            string contenType;
            if (string.Equals(extension, ".pdf", StringComparison.CurrentCultureIgnoreCase))
            {
                contenType = "application/pdf";
            }
            else
            {
                contenType = "application/octet-stream";
            }
            return File(new FileInfo($"Temp/{fileName}").OpenRead(), contenType);
            //return new FileStreamResult(new FileInfo($"Temp/{fileName}").OpenRead(), MediaTypeHeaderValue.Parse("application/pdf"));
            //Span<byte> buffers = new Span<byte>();
            //using (var fin = new FileInfo($"Temp/{fileName}").OpenRead())
            //{
            //    fin.Read(buffers);
            //}

            //return new FileContentResult(buffers.ToArray(), MediaTypeHeaderValue.Parse("application/pdf"));
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
